package com.zpb.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zpb.framework.domain.ApiResult;
import com.zpb.framework.domain.dto.CategoryAddDto;
import com.zpb.framework.domain.dto.CategoryQueryDto;
import com.zpb.framework.domain.dto.CategoryUpdateDto;
import com.zpb.framework.domain.entity.Article;
import com.zpb.framework.domain.entity.Category;
import com.zpb.framework.domain.enums.ResultCodeEnum;
import com.zpb.framework.domain.vo.CategoryAdminVo;
import com.zpb.framework.domain.vo.CategoryVo;
import com.zpb.framework.domain.vo.PageVo;
import com.zpb.framework.mapper.ArticleMapper;
import com.zpb.framework.mapper.CategoryMapper;
import com.zpb.framework.service.CategoryService;
import com.zpb.framework.utils.BeanCopyUtil;
import com.zpb.framework.utils.NumConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: zhaopengbo
 * @Date: 2025/9/10
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ArticleMapper articleMapper;
    @Override
    public ApiResult<List<CategoryVo>> getList() {
        LambdaQueryWrapper<Article> articleWrapper = new LambdaQueryWrapper<>();
        articleWrapper
                .eq(Article::getDelFlag, NumConst.ZERO) //配置中已进行配置，可以不写
                .eq(Article::getStatus, NumConst.ZERO);
        List<Article> allArticle = articleMapper.selectList(articleWrapper);
        List<Category> categoryList = new ArrayList<>();
        allArticle.stream()
                .map(Article::getCategoryId)
                .distinct()
                .forEach(id->{
                    categoryList.add(categoryMapper.selectById(id));
                    categoryList.stream()
                            .filter(category -> Integer.valueOf(category.getStatus()).equals(NumConst.ONE))
                            .collect(Collectors.toList());
                });
        List<CategoryVo> categoryVos = BeanCopyUtil.copyBeanList(categoryList, CategoryVo.class);
        return ApiResult.success(categoryVos);
    }

    @Override
    public ApiResult<List<CategoryVo>> getAll() {
        List<Category> categoryList = categoryMapper.selectList(null);
        List<CategoryVo> categoryVos = BeanCopyUtil.copyBeanList(categoryList, CategoryVo.class);
        return ApiResult.success(categoryVos);
    }

    @Override
    public List<Category> getCategoryList(){
        return categoryMapper.selectList(null);
    }

    @Override
    public ApiResult<PageVo> getCategoryPage(Integer pageNum, Integer pageSize, CategoryQueryDto categoryQueryDto) {

        Page<Category> categoryPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Category> categoryWrapper = new LambdaQueryWrapper<>();
        categoryWrapper.like(Objects.nonNull(categoryQueryDto.getName())&& !categoryQueryDto.getName().isEmpty(),Category::getName,categoryQueryDto.getName())
                .eq(Objects.nonNull(categoryQueryDto.getStatus()), Category::getStatus,categoryQueryDto.getStatus());
        page(categoryPage, categoryWrapper);
        List<Category> categoryList = categoryPage.getRecords();
        List<CategoryAdminVo> categoryAdminVos = BeanCopyUtil.copyBeanList(categoryList, CategoryAdminVo.class);
        PageVo pageVo = new PageVo(categoryPage.getTotal(),categoryAdminVos);
        return ApiResult.success(pageVo);
    }
    @Override
    public ApiResult addCategory(CategoryAddDto categoryAddDto){
        Category category = BeanCopyUtil.copyBean(categoryAddDto, Category.class);
        int result = categoryMapper.insert(category);
        if(result > 0){
            return ApiResult.success();
        }
        return ApiResult.error(ResultCodeEnum.ADD_FAILURE);
    }

    @Override
    public ApiResult<CategoryAdminVo> getCategoryById(Long id) {
        Category category = categoryMapper.selectById(id);
        if(Objects.isNull(category)){
            return ApiResult.failure(ResultCodeEnum.NO_CATEGORY);
        }
        CategoryAdminVo categoryAdminVo = BeanCopyUtil.copyBean(category, CategoryAdminVo.class);
        return ApiResult.success(categoryAdminVo);
    }

    @Override
    public ApiResult updateCategory(CategoryUpdateDto categoryUpdateDto) {
        Category category = BeanCopyUtil.copyBean(categoryUpdateDto, Category.class);
        int result = categoryMapper.updateById(category);
        if(result > 0){
            return ApiResult.success();
        }
        return ApiResult.error(ResultCodeEnum.UPDATE_FAILURE);
    }

    @Override
    public ApiResult delCategoryById(Long id) {
        Category category = categoryMapper.selectById(id);
        if(Objects.isNull(category)){
            return ApiResult.failure(ResultCodeEnum.NO_CATEGORY);
        }
        int result = categoryMapper.deleteById(id);
        if(result > 0){
            return ApiResult.success();
        }
        return ApiResult.error(ResultCodeEnum.DEL_FAILURE);

    }
}
